package com.web.contract.service;

import com.web.account.entity.Account;
import com.web.account.service.AccountService;
import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.enums.BaseStatus;
import com.web.contract.entity.Contract;
import com.web.contract.mapper.ContractMapper;
import com.web.customer.entity.Customer;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.service.PendingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-06-08 2024/6/8
 */
@Service
public class ContractService implements PendingAmend {

    @Resource
    private ContractMapper contractMapper;

    @Resource
    private PendingListService pendingService;

    @Resource
    private AccountService accountService;


    @Override
    @Transactional
    public BaseResult rejectAddPending(PendingList pendingTxn) {
       //根据PendingNumber查询出Contract
        Contract contract = contractMapper.getContractByPendingNumber(pendingTxn.getPendingNum());
        if(contract == null){
            return new BaseResult(BaseStatus.CONTRACT_DATA_NOT_EXIST.getCode(),BaseStatus.CONTRACT_DATA_NOT_EXIST.getDesc());
        }

        //如果存在就将Contract数据进行物理删除
        if(contractMapper.physicsDeleteContract(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //修改审批数据的状态
        if(pendingService.updatePendingTxn(PendingList.makeReject(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult approveAddPending(PendingList pendingTxn) {
        //根据PendingNumber查询出Contract
        Contract contract = contractMapper.getContractByPendingNumber(pendingTxn.getPendingNum());
        if(contract == null){
            return new BaseResult(BaseStatus.CONTRACT_DATA_NOT_EXIST.getCode(),BaseStatus.CONTRACT_DATA_NOT_EXIST.getDesc());
        }

        //根据Currency、Buy_Sell、客户号查询出账户
        Account buyAccount = accountService.getBuyAccountByCurrency(contract.getBuyCurrency(),contract.getCustomerNo());
        Account sellAccount = accountService.getSellAccountByCurrency(contract.getSellCurrency(),contract.getCustomerNo());

        if(buyAccount == null || sellAccount== null){
            return new BaseResult(BaseStatus.ACCOUNT_DATA_EXCEPTION.getCode(),BaseStatus.ACCOUNT_DATA_EXCEPTION.getDesc());
        }

        //判断合约中的Sell扣账账户余额是否足够
        System.out.println("contract: " + contract.getSellAmount());
        System.out.println("Account: " + sellAccount.getAccountBalance());
        if(sellAccount.getAccountBalance().compareTo(contract.getSellAmount()) < 0){
            return new BaseResult(BaseStatus.SELL_AMOUNT_NOT_ENOUGH.getCode(),BaseStatus.SELL_AMOUNT_NOT_ENOUGH.getDesc());
        }

        //将Contract的APPROVED栏位标记为 Y,代表审批通过,同时将PendingNumber设置为null
        if(contractMapper.updateApprove(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //将Pending表中的status修改为APP,代表审批通过
        if(pendingService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //扣账账户和入账账户增加/减少对应的金额
        BigDecimal addToBalance = buyAccount.getAccountBalance().add(contract.getBuyAmount());
        buyAccount.setAccountBalance(addToBalance);

        BigDecimal subtractToBalance = sellAccount.getAccountBalance().subtract(contract.getSellAmount());
        sellAccount.setAccountBalance(subtractToBalance);

        accountService.increaseAmount(buyAccount);

        accountService.reduceAmount(sellAccount);

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

    @Override
    public BaseResult rejectDeletePending(PendingList pendingTxn) {
        return null;
    }

    @Override
    public BaseResult approveDeletePending(PendingList pendingTxn) {
        return null;
    }

    @Override
    public BaseResult rejectModifyPending(PendingList pendingTxn) {
        return null;
    }

    @Override
    public BaseResult approveModifyPending(PendingList pendingTxn) {
        return null;
    }


    public Integer generateContract(Contract contract) {
        return contractMapper.addContract(contract);
    }

    public List<Contract> getContratList(Contract contract) {
        return contractMapper.getContractList(contract);
    }

    public List<Contract> getContratWithNor() {
        return contractMapper.getContractByNor();
    }


    public Integer settlementContract(List<Contract> contractList) {
        return contractMapper.updateStatus(contractList);
    }
}
